package com.investment.advisor.controller;

import com.investment.advisor.dto.UserDTO;
import com.investment.advisor.dto.RegisterRequest;
import com.investment.advisor.dto.LoginRequest;
import com.investment.advisor.dto.LogoutRequest;
import com.investment.advisor.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/auth")
public class AuthController {

    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public ResponseEntity<UserDTO> register(@RequestBody RegisterRequest request) {
        return ResponseEntity.ok(userService.register(request));
    }

    @PostMapping("/login")
    public ResponseEntity<UserDTO> login(@RequestBody LoginRequest request, HttpServletRequest httpRequest) {
        return ResponseEntity.ok(userService.login(request, httpRequest));
    }

    @PostMapping("/logout/{userId}")
    public ResponseEntity<Void> logout(@PathVariable Long userId, @RequestBody(required = false) LogoutRequest request) {
        String deviceInfo = request != null ? request.getDeviceInfo() : null;
        userService.logout(userId, deviceInfo);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/online/users")
    public ResponseEntity<List<UserDTO>> getOnlineUsers() {
        return ResponseEntity.ok(userService.getOnlineUsers());
    }

    @GetMapping("/online/count")
    public ResponseEntity<Long> getOnlineUserCount() {
        return ResponseEntity.ok(userService.getOnlineUserCount());
    }
} 